package it.blueocean.acanto.security.authenticationManager.service.impl;

import it.blueocean.acanto.entities.platform.DBUser;
import it.blueocean.acanto.security.authenticationManager.service.UserManagerService;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

public class UserManagerServiceImpl implements UserManagerService
{
	private final static Logger logger = LoggerFactory.getLogger(UserManagerServiceImpl.class);
	
	@PersistenceContext(type = PersistenceContextType.EXTENDED)
	private EntityManager entityManager;
	
	@Override
	public DBUser getUser(String principal, String credentials, WebAuthenticationDetails details)
	{
		if(logger.isDebugEnabled())
		{
			logger.debug("principal [" + principal + "]");
		}
		
		DBUser securityUser = (DBUser) entityManager.createQuery("from DBUser where username = :username").setParameter("username", principal).getSingleResult();
		logger.debug("Retrived MenuItem list" + securityUser);
		
		return securityUser;
	}

}
